(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("setlDetailFxRateService", setlDetailFxRateService);
	setlDetailFxRateService.$inject = ['setlDetailStyleService', 'arithService', 'fxCrossRateService'];
	function setlDetailFxRateService(setlDetailStyleService, arithService, fxCrossRateService) {
		
		/**
		 * enable fields but need to check the fx rate type: accept input and give default values, show required labels and icons
		 * @param itemNo
		 */
		function enableFxRateAndFxRate2(setlDetail, baseCcy){
			setlDetail.fxRateSpan_Show = true;
			var fxRateType = setlDetail.fxRateType;
		    var fxRateType2 = setlDetail.fxRateType2;
		    var itemccy = setlDetail.itemCcy;
		    var payccy = setlDetail.payCcy;
		    if(itemccy != baseCcy && payccy != baseCcy) {                 
		    	setlDetail.fxRateType_Readonly = false;
		        if(fxRateType == "S" || fxRateType == "B")                                                
		        	setlDetail.fxRate_Readonly = true;
		            
		        setlDetail.fxRateType2_Readonly = false;
		        if(fxRateType2 == "S" || fxRateType2 == "B")
		        	setlDetail.fxRate2_Readonly = true;
		    }
		    if(itemccy != baseCcy && payccy == baseCcy)  {
		    	setlDetail.fxRateType2_Readonly = false;
		        if(fxRateType2 == "S" || fxRateType2 == "B")                                                
		        	setlDetail.fxRate2_Readonly = true;

		        setlDetail.fxRateType = "";
		        setlDetail.fxRateType_Readonly = true;

		        setlDetail.fxRate = "";
		        setlDetail.fxRate_Readonly = true;
		    }
		    if(itemccy == baseCcy && payccy != baseCcy )  {
		    	setlDetail.fxRateType_Readonly = false;
		        if(fxRateType == "S" || fxRateType == "B")                                                
		        	setlDetail.fxRate_Readonly = true;

		        setlDetail.fxRateType2 = "";
		        setlDetail.fxRateType2_Readonly = true;

		        setlDetail.fxRate2 = "";
		        setlDetail.fxRate2_Readonly = true;
		    }
		    //display the fxrate ccy text
		    setlDetail.fxRate1CcyFrom = "";
		    setlDetail.fxRate2CcyFrom = "";
		    if(payccy != baseCcy)
		    	setlDetail.fxRate1CcyFrom = payccy;
			if(itemccy != baseCcy)
				setlDetail.fxRate2CcyFrom = itemccy;
		}
		
		/***************************************************************
		 * Initialize settlement details" fx rateTypes, fxRate, tax
		 * setlDetail - the index of child item in settlement details array
		 * itemccy == payccy: itemccy != payccy && itemccy == bccy;
		 * itemccy != payccy && payccy == bccy; itemccy != payccy &&
		 * payccy !=bccy && itemccy != bccy;
		 **************************************************************/
		function initSettlementDetailRateAndTax(settlement, setlDetail) {
			if (setlDetail.payCcy == setlDetail.itemCcy) {
				setlDetailStyleService.disableFxRateAndFxRate2(setlDetail);
				// 2016-9-9 disabled fxCrossRateFlag
				fxCrossRateService.FxCrossRate_Util.disableCrossRate(setlDetail);
			} else {
				fxCrossRateService.FxCrossRate_Util.enableCrossRate(setlDetail);
				// if it is not cross rate, we need the normal fx rate section,
				// the following behaviors are visible on page
				// if it is cross rate, the normal fx rate section is hidden, so
				// the following behaviors are invisible. so keeping them have
				// no side effects
				if (fxCrossRateService.FxCrossRate_Util.isCrossRate(setlDetail)) {
					setlDetailStyleService.disableFxRateAndFxRate2(setlDetail);
				} else {
					enableFxRateAndFxRate2(setlDetail, settlement.site.baseCcy);
				}
			}
			// tax
			setlDetailStyleService.processTaxWaiveLabel(setlDetail);
			setlDetailStyleService.fxCrossStyle(setlDetail);
		}
		
		/**
		 * convert to base ccy rate
		 */
		function getToBaseCcyFxRateValue(crDr, ccyType, bccy, fromCcy){
			if(bccy == fromCcy)
				return 1;
			var fxRateType = "";
			if(ccyType == "paydetail"){
				fxRateType = "K";
			}else{
			    if(crDr == "C"){//item or pay
			    	if(ccyType == "item")
			      		fxRateType = "B";
			      	else// pay
			      		fxRateType = "S";
			    }
			    if(crDr == "D"){
			    	if(ccyType == "item")
			      		fxRateType = "S";
			      	else// pay
			      		fxRateType = "B";
			    }
			}
		    var rm2 = getFxRate(settlement, bccy, fromCcy, fxRateType);
		    return (rm2[0]-0);	
		}
		
		
		/**
		 * get fx rate and multiply flag from FX Rate Array base on from currency
		 */
		function getFxRate(settlement, bccy, ccy, rateType){	
			var site = settlement.site;
			var isFxMultiply = site.isFxMultiply;
			var fxRates = settlement.fxRates;
		    var rm = new Array();
		    for(var tmp1 in fxRates) {
		    	var fxRate = fxRates[tmp1];
		        if(fxRate.ccyFrom == ccy && fxRate.ccyTo == bccy ) {
		        	if(isFxMultiply == 'Y'){
		        		rm[1] = true;
		        	}
		            if(rateType == "B")
		                rm[0] = fxRate.buyRate;                                                      
		            if(rateType == "S")
		                rm[0] = fxRate.sellRate;
		            if(rateType == "K")
		            	rm[0] = fxRate.bookRate;
		            break;
		        }                                               
		    }
		    if (bccy == ccy) {
//		    	if (fxRateArry[0] != null) {
//		    		rm[1] = fxRateArry[0].isMultiply;
		    		rm[0] = 1;
//		    		return rm;
//		    	}
		    }
		    return rm;
		}
		
		/**
		 * assign default values to fxRateType, fxRate, fxRateType2 and fxRate2
		 * @param itemNo
		 */
		function assignDefaultValueToFxRateAndFxRate2(settlement, setlDetail){
			var itemccy = setlDetail.itemCcy;
		    var payccy = setlDetail.payCcy;
			var bccy = settlement.site.baseCcy;
			var crdr = setlDetail.crDrFlag;

			if(itemccy != bccy && payccy != bccy){
				if(crdr=="C") {
					setlDetail.fxRateType = "S";
				}
				if(crdr=="D"){
					setlDetail.fxRateType = "B";
				}
				var rm1 = getFxRate(settlement, bccy, payccy, setlDetail.fxRateType);
				setlDetail.fxRate = rm1[0];
			
				if(crdr =="C"){
					setlDetail.fxRateType2 = "B";
				}
				if(crdr == "D"){
					setlDetail.fxRateType2 = "S";                                                        
				}
			    var rm2 = getFxRate(settlement, bccy, itemccy, setlDetail.fxRateType2);
			    setlDetail.fxRate2 = rm2[0];           
			}
			if(payccy == bccy && itemccy != bccy){
			    if(crdr == "C"){
			    	setlDetail.fxRateType2 = "B";
			    }
			    if(crdr == "D"){
			    	setlDetail.fxRateType2 = "S";
			    }
			    var rm3 = getFxRate(settlement, bccy, itemccy, setlDetail.fxRateType2);
			    setlDetail.fxRate2 = rm3[0];
			    //fxRate is disabled
			}
			if(payccy != bccy && itemccy == bccy){
			    if(crdr == "C"){
			    	setlDetail.fxRateType = "S";
			    }
			    if(crdr == "D"){
			    	setlDetail.fxRateType = "B";
			    }
			    var rm4 = getFxRate(settlement, bccy, payccy, setlDetail.fxRateType);
			    setlDetail.fxRate = rm4[0];
			    //fxRate2 is disabled
			}
		}
		
		return {
			assignDefaultValueToFxRateAndFxRate2 : assignDefaultValueToFxRateAndFxRate2,
			enableFxRateAndFxRate2 : enableFxRateAndFxRate2,
			getToBaseCcyFxRateValue : getToBaseCcyFxRateValue,
			initSettlementDetailRateAndTax : initSettlementDetailRateAndTax
		};
	}
})(angular);
